package com.wodsy.phoenix.search.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.wodsy.phoenix.datadao.base.IBaseDao;
import com.wodsy.phoenix.datadao.goodsc.dao.SearchGoodsDao;
import com.wodsy.phoenix.dataobject.base.PhoenixResponseWrapper;
import com.wodsy.phoenix.dataobject.goodsc.model.search.SearchGoods;
import com.wodsy.phoenix.dataobject.goodsc.model.search.SearchResultList;
import com.wodsy.phoenix.dataservice.base.AbstractBaseService;
import com.wodsy.phoenix.search.GoodsSearch;

@Service(interfaceClass = GoodsSearch.class, interfaceName = "com.wodsy.phoenix.search.GoodsSearch")
public class GoodsSearchImpl extends AbstractBaseService<SearchGoods, Long>
		implements GoodsSearch {

	@Autowired
	private SearchGoodsDao searchGoodsDao;

	@Override
	protected IBaseDao<SearchGoods, Long> getIBaseDao() {
		// TODO Auto-generated method stub
		return searchGoodsDao;
	}

	@Override
	@Transactional(readOnly = true, rollbackFor = Throwable.class)
	public PhoenixResponseWrapper<SearchResultList> searchGoodsBykeyVSDefault(
			String key, int pno, int psize, int min, int max, boolean price) {
		List<SearchGoods> searchlist = searchGoodsDao.searchGoodsBykey(key,
				min, max);
		SearchResultList sr = new SearchResultList(getSubListPage(searchlist,
				pno * psize, (pno + 1) * psize), searchlist.size(), pno,
				searchlist.size() / psize);
		return new PhoenixResponseWrapper<SearchResultList>(sr);
	}

	@Override
	@Transactional(readOnly = true, rollbackFor = Throwable.class)
	public PhoenixResponseWrapper<SearchResultList> searchGoodsBykeyVSPopularity(
			String key, int pno, int psize, int min, int max, boolean price) {
		// searchGoodsDao.index();

		List<SearchGoods> searchlist = searchGoodsDao.searchGoodsBykey(key,
				min, max);

		Collections.sort(searchlist, new Comparator<SearchGoods>() {

			public int compare(SearchGoods arg0, SearchGoods arg1) {
				// 人气排序
				int result = arg0.getGoodsSaleProperty().getPopularity()
						.compareTo(arg1.getGoodsSaleProperty().getPopularity());

				// 人气相同时
				if (result == 0) {
					//
					result = arg0.getAddTime().compareTo(arg0.getAddTime());
				}
				return result;
			}
		});

		SearchResultList sr = new SearchResultList(getSubListPage(searchlist,
				pno * psize, (pno + 1) * psize), searchlist.size(), pno,
				searchlist.size() / psize);
		return new PhoenixResponseWrapper<SearchResultList>(sr);
	}

	@Override
	@Transactional(readOnly = true, rollbackFor = Throwable.class)
	public PhoenixResponseWrapper<SearchResultList> searchGoodsBykeyVSSales(
			String key, int pno, int psize, int min, int max, boolean price) {

		List<SearchGoods> searchlist = searchGoodsDao.searchGoodsBykey(key,
				min, max);

		Collections.sort(searchlist, new Comparator<SearchGoods>() {

			public int compare(SearchGoods arg0, SearchGoods arg1) {
				// 销量排序
				int result = arg0.getGoodsSaleProperty().getSales()
						.compareTo(arg1.getGoodsSaleProperty().getSales());

				// 销量相同时
				if (result == 0) {
					// 浏览量排序
					result = arg0.getGoodsSaleProperty().getViews()
							.compareTo(arg0.getGoodsSaleProperty().getViews());

					// 浏览量相同时
					if (result == 0) {
						// 上架时间排序
						result = arg0.getAddTime().compareTo(arg1.getAddTime());
					}
				}
				return result;
			}
		});

		SearchResultList sr = new SearchResultList(getSubListPage(searchlist,
				pno * psize, (pno + 1) * psize), searchlist.size(), pno,
				searchlist.size() / psize);
		return new PhoenixResponseWrapper<SearchResultList>(sr);
	}

	@Override
	@Transactional(readOnly = true, rollbackFor = Throwable.class)
	public PhoenixResponseWrapper<SearchResultList> searchGoodsBykeyVSPrice(
			String key, int pno, int psize, int min, int max,
			final boolean price) {
		List<SearchGoods> searchlist = searchGoodsDao.searchGoodsBykey(key,
				min, max);

		Collections.sort(searchlist, new Comparator<SearchGoods>() {

			public int compare(SearchGoods arg0, SearchGoods arg1) {
				// 价格排序
				int result = 0;

				if (price) {
					result = arg1.getPrice().compareTo(arg0.getPrice());
				} else {
					result = arg0.getPrice().compareTo(arg1.getPrice());
				}
				// 价格相同时
				if (result == 0) {
					// 上架时间排序
					result = arg0.getAddTime().compareTo(arg1.getAddTime());
				}

				return result;
			}
		});

		for (SearchGoods sg : searchlist) {
			System.out.println("===================" + sg.getPrice());
		}
		SearchResultList sr = new SearchResultList(getSubListPage(searchlist,
				pno * psize, (pno + 1) * psize), searchlist.size(), pno,
				searchlist.size() / psize);
		return new PhoenixResponseWrapper<SearchResultList>(sr);
	}

	/**
	 * 
	 * @Title: getSubListPage
	 * @Description: list 分页
	 * @param
	 * @return List<SearchGoods> 返回类型
	 *
	 */
	public static List<SearchGoods> getSubListPage(List<SearchGoods> list,
			int start, int end) {
		if (list == null || list.isEmpty()) {
			return null;
		}
		int startIndex = start;
		int endIndex = start + end;
		if (startIndex > endIndex || startIndex > list.size()) {
			return null;
		}
		if (endIndex > list.size()) {
			endIndex = list.size();
		}
		return list.subList(startIndex, endIndex);
	}

	public static void main(String[] args) {
		com.alibaba.dubbo.container.Main.main(args);
	}

}
